/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ik.pbs.Manejadores;

import com.ik.pbs.Encapsulados.Profecion;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Malkavian
 */
public class ManejadorProfecion extends Manejador {

    public static ManejadorProfecion manager = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");
    private SimpleDateFormat shf = new SimpleDateFormat("HH:mm:ss");

    public static ManejadorProfecion getInstance() {
        if (manager == null) {
            manager = new ManejadorProfecion();
        }

        return manager;
    }

    public void ManejadorProfecion() {
    }

    public ArrayList<Profecion> getProfecion(Connection con) {
        ArrayList<Profecion> arregloProfecion = new ArrayList();

        ResultSet rd = null;
        PreparedStatement state;

        String query = "select * from profeciones order by id";

        try {
            state = con.prepareCall(query);
            rd = state.executeQuery();

            while (rd.next()) {
                Profecion categoria = new Profecion();
                categoria.setId(rd.getInt("id"));
                categoria.setProfecion(rd.getString("profecion"));

                arregloProfecion.add(categoria);
            }

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return arregloProfecion;
    }

    public Profecion getProfecion(Connection con, int codigo) {
        Profecion categoria = new Profecion();

        ResultSet rd = null;
        PreparedStatement state;

        String query = "select * from profeciones where id = ?";

        try {
            state = con.prepareCall(query);
            state.setInt(1, codigo);
            rd = state.executeQuery();

            while (rd.next()) {

                categoria.setId(rd.getInt("id"));
                categoria.setProfecion(rd.getString("profecion"));
            }

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return categoria;
    }

    public boolean insertarGenero(Connection con, Profecion categoria) {
        PreparedStatement state;

        String query = "insert into profeciones values(?,?)";
        try {


            state = con.prepareStatement(query); 
            state.setInt(1, categoria.getId());
            state.setString(2, categoria.getProfecion());

            state.execute();
            return true;
        } catch (SQLException ex) {
            return actualizarGenero(con, categoria);
        }


    }

    public boolean actualizarGenero(Connection con, Profecion categoria) {
        PreparedStatement state;

        String query = "update profecion set profecion = ? where id = ?";

        try {
            state = con.prepareStatement(query);
            state.setInt(2, categoria.getId());
            state.setString(1, categoria.getProfecion());

            state.execute();
            return true;

        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            return false;
        }


    }

    public int getNext(Connection con) {
        try {
            ResultSet rd = null;
            PreparedStatement state;
            String query = "select max(id) as max from profeciones";

            state = con.prepareStatement(query);
            rd = state.executeQuery();

            if (!rd.next()) {
                return 1;
            } else {
                return (rd.getInt("max") + 1);
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return 0;
    }
    
    
}
